#include "car_power_range.hpp"
#include "csv_struct.hpp"
#include "file_list.hpp"


car_power_range_csv::car_power_range_csv(): file(file_list::car_power_range_filename())
{}


car_power_range_struct_csv const car_power_range_csv::find(int _id)
{
		csv_struct csv_file(file_list::car_power_range_filename());

		while(_id && !csv_file.eof())
		{
			++csv_file;
			car_power_range_struct_csv row(csv_file.get_line());
			if(row.id() == _id)
			{	
				return row;				
			}	
		}

		car_power_range_struct_csv car_power_range_struct_csv_null;
		return car_power_range_struct_csv_null;
}

car_power_range_struct_csv const car_power_range_csv::find(int _min, int _max)
{
		csv_struct csv_file(file_list::car_power_range_filename());

		while(!csv_file.eof())
		{
			++csv_file;
			car_power_range_struct_csv row(csv_file.get_line());
			if(row.min_power() == _min && row.max_power() == _max)
			{	
				return row;
			}	
		}

		car_power_range_struct_csv car_power_range_struct_csv_null;
		return car_power_range_struct_csv_null;
}


int car_power_range_csv::find_power_range_id(int _range[])
{
		return static_cast<car_power_range_struct_csv>(car_power_range_csv::find(_range[0], _range[1])).id();		
}


/*
	inline bool exist(int _id)
	{
		car_power_range_struct_csv r;
		return find(_id, r);
	}

	inline bool exist(int _min, int _max)
	{
		car_power_range_struct_csv r;
		return find(_min, _max, r);
	}
*/


/*
	inline int min_power(int _id)
	{
		car_power_range_struct_csv r;
		
		if(find(_id, r))
		{
			car_power_csv p(file_list::car_power_filename());
			return p.power(r.min_power());
		}
		return 0;
	}

	inline int max_power(int _id)
	{
		car_power_range_struct_csv r;
		
		if(find(_id, r))
		{
			car_power_csv p(file_list::car_power_filename());
			return p.power(r.max_power());
		}
		return 0;
	}

       	inline std::string const descr(int _id)
	{	
		car_power_range_struct_csv r;

		if(find(_id, r))
		{	
			return r.notes();
		}
		return "";
	}

	inline std::string const filename() const { return this->file; }
        inline car_power_range_struct_csv const init_line(std::vector<std::string>& s) 
	{ 
		car_power_range_struct_csv row(s);
		this->line = row;

		return this->line; 
	}

	inline  car_power_range_struct_csv const get_line() const { return this->line; }
*/

/*
	static inline bool find(float _power, int range[])
	{
		csv_struct csv_file(file_list::car_power_range_filename());
		bool is_in_range;

		while(!csv_file.eof())
		{
			++csv_file;
			car_power_struct_csv row(csv_file.get_line());
			if(row.power() <= _power && range[0] <= row.power())
			{
				range[0] = _power;  is_in_range = true;
			}
			else if(row.power() >= _power && row.power() <= range[1] )
			{	
				range[1] = row.power();	is_in_range = true;
			}	
		}
		return false;
	}
*/